package jp.sourceforge.akjrcp.text_douga.item;

import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.math.BigDecimal;

import jp.sourceforge.akjrcp.text_douga.DougaPreference;
import jp.sourceforge.akjrcp.text_douga.DougaSystem;
import jp.sourceforge.akjrcp.text_douga.ImageObject;
import jp.sourceforge.akjrcp.text_douga.SystemEnvironment;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/*
 * normal image
 */
public class DrawingItem extends AbstractItem{
	public static Log log=LogFactory.getLog(DrawingItem.class);
	
	protected int color=0xff0000;	//white
	protected boolean fill=false;
	
	public static final int TYPE_RECT=0;
	public static final int TYPE_OVAL=1;
	public static final int TYPE_LINE=2;
	protected int drawType=TYPE_RECT;
	protected int lineSize=1;
	protected boolean antialiase=true;
	public int getColor() {
		return color;
	}

	public void setColor(int color) {
		this.color = color;
	}

	public boolean isFill() {
		return fill;
	}

	public void setFill(boolean fill) {
		this.fill = fill;
	}

	public int getDrawType() {
		return drawType;
	}

	public void setDrawType(int drawType) {
		this.drawType = drawType;
	}

	public int getLineSize() {
		return lineSize;
	}

	public void setLineSize(int lineSize) {
		this.lineSize = lineSize;
	}

	public DrawingItem(){
	}
	
	
	
	public String toString(){
		return "DrawingItem:"+super.toString()+" color="+color;
	}
		//location
	public int getZ() {
		// small is firster
		return z;
	}

	public boolean writeFrame(DougaPreference preference,ImageObject baseImage,int frame)throws IOException{
		boolean written=false;
		if(duration==DougaSystem.FOREEVER){//maybe this is background image
			written=true;
		}
		double frameTime=getFrameTime(frame,preference.getFps());
		if(written==false){
			written=isInFrame(preference,frame);
			}
		
		if(written){
		
			if(getWidth()>0 && getHeight()>0){
			//drawing;
			Rectangle drawRect=getDrawRect(preference);
			
			
			
			Graphics2D g2=(Graphics2D) baseImage.getImage().getGraphics();
			float tp=getFrameTransparent(preference,frameTime);
			g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, tp));//use tp!
			int mx=getMovedX(preference,frameTime);
			int my=getMovedY(preference,frameTime);
			int drawW=drawRect.width+getMovedWidth(preference,frameTime);
			int drawH=drawRect.height+getMovedHeight(preference,frameTime);
			g2.setStroke(new BasicStroke(lineSize));
			g2.setColor(new Color(color));
			if(antialiase){
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
			             RenderingHints.VALUE_ANTIALIAS_ON);
			}else{
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
			             RenderingHints.VALUE_ANTIALIAS_OFF);
			}
			if(drawW>0 && drawH>0){
			if(fill){
				if(drawType==TYPE_RECT){
					g2.fillRect(drawRect.x+mx, drawRect.y+my, drawW, drawH);
				}else if(drawType==TYPE_LINE){
					g2.drawLine(drawRect.x+mx, drawRect.y+my, drawW, drawH);
				}else{
					g2.fillOval(drawRect.x+mx, drawRect.y+my, drawW, drawH);
				}
			}else{
				if(drawType==TYPE_RECT){
					g2.drawRect(drawRect.x+mx, drawRect.y+my, drawW, drawH);
				}else if(drawType==TYPE_LINE){
					g2.drawLine(drawRect.x+mx, drawRect.y+my, drawW, drawH);
				}else{
					g2.drawOval(drawRect.x+mx, drawRect.y+my, drawW, drawH);
				}
				}
			}
			}
		}
		
		return written;
	}
	
	public Rectangle getDrawRect(DougaPreference preference){
		return new Rectangle(getX(),getY(),getWidth(),getHeight());
	}

	public boolean isAntialiase() {
		return antialiase;
	}

	public void setAntialiase(boolean antialiase) {
		this.antialiase = antialiase;
	}



	
}
